import { ref, type Ref } from "vue";

// 编写一个通过秒数控制数字从0增长到一个目标值的方法
export function incrementNumber(targetNumber: number, duration: number, sum: Ref<number>) {
  const increment = targetNumber / (duration / 50);
  let currentNumber = 0;

  const interval = setInterval(() => {
    currentNumber += increment;
    if (currentNumber >= targetNumber) {
      currentNumber = targetNumber;
      clearInterval(interval);
    }
    sum.value = Math.round(currentNumber);
  }, 50);
}

export function incrementNumberComputed(targetNumber: number, duration: number) {
  const sum = ref(0)
  const increment = targetNumber / (duration / 50);
  let currentNumber = 0;

  const interval = setInterval(() => {
    currentNumber += increment;
    if (currentNumber >= targetNumber) {
      currentNumber = targetNumber;
      sum.value = currentNumber;
      clearInterval(interval);
    } else {
      sum.value = Math.round(currentNumber);
    }
  }, 50);

  return sum
}